package com.glela.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.glela.platform.constant.ErrorEnum;
import com.glela.platform.model.User;
import com.glela.platform.model.UserJournal;
import com.glela.platform.model.UserPromotion;
import com.glela.platform.model.request.RequestUserModel;
import com.glela.platform.model.response.ResponseBaseModel;
import com.glela.platform.model.response.ResponsePageModel;
import com.glela.platform.model.response.ResponseResultModel;
import com.glela.platform.model.response.ResponseUserJournal;
import com.glela.platform.model.response.ResponseUserPromotion;
import com.glela.platform.service.UnifiedTreatmentService;
import com.glela.platform.service.UserJournalService;
import com.glela.platform.service.UserService;
import com.glela.platform.vo.ResponseUserJournalPageVo;
import com.glela.platform.vo.UserJournalVo;

@Controller
@RequestMapping(value = "/userJournal")
public class UserJournalController {

	private static Logger logger = LoggerFactory.getLogger(UserJournalController.class);

	@Autowired
	private UserService userService;
	@Autowired
	private UserJournalService userJournalService;
	@Autowired
	private UnifiedTreatmentService unifiedTreatmentService;

	/**
	 * 我的钱包中的妆币及收支明细
	 * 
	 * @author 卫耀华
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/myZhuangBi", method = RequestMethod.POST, consumes = "application/json")
	public ResponseEntity<?> myZhuangBi(@RequestBody RequestUserModel request) {
		try {
			/*1. 验证用户存在*/
			if (request.getUserId() == 0L) {
				return new ResponseEntity<>(new ResponseBaseModel(ErrorEnum.C20133), HttpStatus.OK);
			}
			User user = userService.getUser(request.getUserId());
			if (null == user) {
				return new ResponseEntity<>(new ResponseBaseModel(ErrorEnum.C20113), HttpStatus.OK);
			}
			/*2. 根据用户id查询用户流水账*/
			List<UserJournal> userJournalList = userJournalService.getUserJournalList(request.getUserId(),
					request.getType(), request.getStartIndex(), request.getPageSize());
			int count = userJournalService.countUserJournal(request.getUserId(), request.getType());
			List<ResponseUserJournal> list = new ArrayList<ResponseUserJournal>();
			for (UserJournal userJournal : userJournalList) {
				ResponseUserJournal model = new ResponseUserJournal();
				BeanUtils.copyProperties(userJournal, model);
				list.add(model);
			}
			// 3 累计获得佣金
			int totalIncome = unifiedTreatmentService.userCommissionTotal(request.getUserId());
			// 4 查询销售额的公用方法
			int totalSales = unifiedTreatmentService.userSalesAmountTotal(request.getUserId());
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("userMoney", user.getMoney());
			map.put("totalIncome", totalIncome);
			map.put("totalSales", totalSales);
			map.put("userJournalList", new ResponsePageModel<ResponseUserJournal>(count, request.getPageSize(),
					request.getPageNum(), list));
			return new ResponseEntity<>(new ResponseResultModel<>(map), HttpStatus.OK);
		} catch (Exception e) {
			logger.error("获取我的钱包中的妆币记录异常", e);
			return new ResponseEntity<>(new ResponseBaseModel(ErrorEnum.C20000), HttpStatus.OK);
		}
	}

	/**
	 * 获取我的优惠券
	 * 
	 * @author 卫耀华
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/getMyCouponList", method = RequestMethod.POST, consumes = "application/json")
	public ResponseEntity<?> getMyCouponList(@RequestBody RequestUserModel request) {
		try {
			if (request.getUserId() == 0L) {
				return new ResponseEntity<>(new ResponseBaseModel(ErrorEnum.C20133), HttpStatus.OK);
			}
			User user = userService.getUser(request.getUserId());
			if (null == user) {
				return new ResponseEntity<>(new ResponseBaseModel(ErrorEnum.C20113), HttpStatus.OK);
			}
			List<UserPromotion> userPromotions = new ArrayList<UserPromotion>();
			List<UserPromotion> userPromotionList = userJournalService.getUserPromotionList(request.getUserId(),
					request.getStartIndex(), request.getPageSize());
			// 如果有多张相同的优惠券，APP上要展开显示，这里要处理一下
			if (userPromotionList != null && userPromotionList.size() > 0) {
				for (UserPromotion userPromotion : userPromotionList) {
					if (userPromotion.getPromotionNumber() > 0) {
						for (int i = 0; i < userPromotion.getPromotionNumber(); i++) {
							userPromotions.add(userPromotion);
						}
					}
				}
			}
			List<ResponseUserPromotion> list = new ArrayList<ResponseUserPromotion>();
			if (userPromotions != null && userPromotions.size() > 0) {
				for (UserPromotion up : userPromotions) {
					ResponseUserPromotion model = new ResponseUserPromotion();
					BeanUtils.copyProperties(up, model);
					model.setWalletTicketId(up.getId());
					list.add(model);
				}
			}
			// 分页总数量
			int count = userJournalService.countUserPromotionByUserId(request.getUserId());
			return new ResponseEntity<>(new ResponseResultModel<>(new ResponsePageModel<ResponseUserPromotion>(count,
					request.getPageSize(), request.getPageNum(), list)), HttpStatus.OK);
		} catch (Exception e) {
			logger.error("获取我的优惠券异常", e);
			return new ResponseEntity<>(new ResponseBaseModel(ErrorEnum.C20000), HttpStatus.OK);
		}
	}

	/**
	 * 达人用户收支明细(程哲权加)
	 * 
	 * @param userModel
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/paymentDetails", method = RequestMethod.POST, consumes = "application/json")
	public ResponseEntity<?> paymentDetails(@RequestBody RequestUserModel request) {
		try {
			// 输入参数校验，判断是否允许为空
			if (request.getUserId() == null || request.getPageNum() == null || request.getPageSize() == null) {
				return new ResponseEntity<>(new ResponseBaseModel(ErrorEnum.C20102), HttpStatus.OK);
			}
			// 根据userId查询用户流水账(分页)
			List<UserJournalVo> userJournalVoList = userJournalService.paymentDetails(request.getUserId(),
					request.getStartIndex(), request.getPageSize());
			int count = userJournalService.countPaymentDetails(request.getUserId());
			ResponseUserJournalPageVo pageModel = new ResponseUserJournalPageVo(count, userJournalVoList);
			return new ResponseEntity<>(new ResponseResultModel<ResponsePageModel<UserJournalVo>>(pageModel),
					HttpStatus.OK);
		} catch (Exception e) {
			logger.error("达人用户收支明细异常:", e);
			return new ResponseEntity<>(new ResponseBaseModel(ErrorEnum.C20000), HttpStatus.OK);
		}
	}
}
